package com.diet263.utils;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.diet263.nfcleedemo.R;

public class WifiAccessPointAdapetr extends BaseAdapter {

	private static final String TAG = "WifiAccessPointAdapetr";
	private List<ScanResult> resultList = new ArrayList<ScanResult>();
	private List<WifiConfiguration> configList = new ArrayList<WifiConfiguration>();
	private Activity activity;

	public WifiAccessPointAdapetr(Activity activity, List<ScanResult> resultList, List<WifiConfiguration> configList) {
		this.activity = activity;
		this.resultList = resultList;
		this.configList = configList;
	}

	@Override
	public int getCount() {
		return this.resultList.size();
	}

	@Override
	public ScanResult getItem(int position) {
		return this.resultList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ScanResult scanResult = getItem(position);
		HolderView holderView = null;
		if (convertView == null) {
			holderView = new HolderView();
			convertView = View.inflate(activity, R.layout.item_list_wifi_access_point, null);
			holderView.tvWifiSSID = (TextView) convertView.findViewById(R.id.tv_wifi_ssid);
			holderView.tvWifiSSIDSaved = (TextView) convertView.findViewById(R.id.tv_wifi_ssid_saved);
			holderView.ivWifiLevel = (ImageView) convertView.findViewById(R.id.iv_wifi_level);
			holderView.ivWifiLock = (ImageView) convertView.findViewById(R.id.iv_wifi_has_pwd);
			convertView.setTag(holderView);
		} else {
			holderView = (HolderView) convertView.getTag();
		}
		boolean isSave = false;
		for (WifiConfiguration wifiConfiguration : configList) {
			if (wifiConfiguration.SSID.replace("\"", "").equals(scanResult.SSID)) {
				isSave = true;
				break;
			}
		}
		if (isSave) {
			holderView.tvWifiSSIDSaved.setVisibility(View.VISIBLE);
		} else {
			holderView.tvWifiSSIDSaved.setVisibility(View.GONE);
		}
		holderView.tvWifiSSID.setText(scanResult.SSID);
		int power = WifiManager.calculateSignalLevel(scanResult.level, 4);
		if (0 == power) {
			holderView.ivWifiLevel.setImageResource(R.drawable.wifi_0);
		} else if (1 == power) {
			holderView.ivWifiLevel.setImageResource(R.drawable.wifi_1);
		} else if (2 == power) {
			holderView.ivWifiLevel.setImageResource(R.drawable.wifi_2);
		} else if (3 == power) {
			holderView.ivWifiLevel.setImageResource(R.drawable.wifi_3);
		}
		boolean locled = false;
		String capabilities = scanResult.capabilities;
		if (!TextUtils.isEmpty(capabilities)) {
			if (capabilities.contains("WPA") || capabilities.contains("wpa")) {
				MyLog.i(TAG, "wpa");
				locled = true;

			} else if (capabilities.contains("WEP") || capabilities.contains("wep")) {
				MyLog.i(TAG, "wep");
				locled = true;
			} else {
				MyLog.i(TAG, "no");
			}
		}

		if (locled) {
			holderView.ivWifiLock.setVisibility(View.VISIBLE);
		} else {
			holderView.ivWifiLock.setVisibility(View.GONE);
		}
		return convertView;
	}

	class HolderView {
		TextView tvWifiSSID;
		TextView tvWifiSSIDSaved;
		ImageView ivWifiLevel;
		ImageView ivWifiLock;		
	}
}
